﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Design;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Security.Permissions;

namespace Dazzle {
    [Serializable]
    public class GChart : ISerializable {
        private ILog logger = LogManager.GetLogger(typeof(GChart));

        internal GChart() { }
        
        #region Name

        private string _name = "chart1";

        [Category("Design")]
        [Description("图表名称，请保证唯一")]
        public string Name {
            get { return _name; }
            set { 
                _name = value;
            }
        }

        #endregion

        #region GraphType

        private string _graphType = "gline";

        [Category("Design")]
        [Description("图表类型")]
        //[TypeConverter(typeof(GraphTypeConverter))]
        public string GraphType {
            get { return _graphType; }
            set { 
                _graphType = value;
            }
        }

        #endregion

        private Win32.tagSIZEL _extent = new Win32.tagSIZEL(15000, 10000);  // HIMETRIC
        [Browsable(false)]
        public Win32.tagSIZEL Extent {
            get { return _extent; }
            internal set {
                _extent = value;
            }
        }

       

        

        #region Parameters

        private ParameterCollection _paramters = new ParameterCollection();
        
        [Category("Configuration")]
        [Description("图表参数设置")]
        [MergableProperty(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [Editor(typeof(ParameterCollectionEditor), typeof(UITypeEditor))]
        public ParameterCollection Parameters {
            get { return _paramters; }
            set { _paramters = value; }
        }

        #endregion

        #region Image

        private Image _image;
        [Browsable(false)]
        internal Image Image {
            get { return _image; }
            set { _image = value; }
        }

        #endregion

        internal void Searilize(Stream writer) {
            BinaryFormatter formatter = new BinaryFormatter();
            
            formatter.Serialize(writer, this);
        }

        internal static GChart Deserialize(Stream reader) {
            BinaryFormatter formatter = new BinaryFormatter();
            return formatter.Deserialize(reader) as GChart;
        }

        #region ISerializable Members

        public GChart(SerializationInfo info, StreamingContext context) {
            this.Name = info.GetString("Name");
            this.GraphType = info.GetString("GraphType");
            this.Extent = new Win32.tagSIZEL(info.GetInt32("ExtentX"), info.GetInt32("ExtentY"));
            //this.FunctionParameters = (MWArray[])info.GetValue("FunctionParameters", typeof(MWArray[]));
            this.Parameters = (ParameterCollection)info.GetValue("Parameters", typeof(ParameterCollection));
            this.Image = (Image)info.GetValue("Image", typeof(Image));
        }


        [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
        public void GetObjectData(SerializationInfo info, StreamingContext context) {
            info.AddValue("Name", this.Name);
            info.AddValue("GraphType", this.GraphType);
            //info.AddValue("FunctionParameters", this.FunctionParameters);
            info.AddValue("Parameters", this.Parameters);
            info.AddValue("ExtentX", this.Extent.cx);
            info.AddValue("ExtentY", this.Extent.cy);
            info.AddValue("Image", this.Image);
        }

        #endregion
    }
}
